package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofanceSaveParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏业务,参数={}",geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        for(int i = 0; i < list.size(); i++){
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            int totalNum = vehicleVOList.size();//获取围栏上绑定的车辆总数
            int availableNum = 0;//定义变量用来保存围栏上的可用车辆数
            for(VehicleVO vehicleVO : vehicleVOList){//遍历此围栏上的车辆列表,判断是否是可用状态
                //如果车辆是可用状态,则计数器加1
                if(vehicleVO.getStatus().equals("1")){//如果车辆状态为空闲
                    availableNum++;//可用车辆数+1
                }
            }
            //给围栏VO补全数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }
    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改围栏状态业务,参数={},{}",id,status);
        Geofence geofence = new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务,参数={}",id);
        /*删除围栏前,需要查此围栏上有没有未移除的车辆*/
        //1.想查车,需要先封装车辆的查询对象
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.注入车辆Mapper对象,调用查询方法
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.判断该围栏上是否有绑定的车辆
        if(list !=null && list.size()>0){
            //4.1如果有车,提示用户围栏上存在未移除的车辆,无法删除
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            //4.2如果没有车,正常删除围栏
            geofenceMapper.deleteGeofence(id);

        }
    }

    @Override
    public void saveGeofence(GeofanceSaveParam geofanceSaveParam) {
        log.debug("保存围栏业务,参数={}",geofanceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofanceSaveParam,geofence);
        geofence.setStatus("1");
        geofence.setCreateTime(new Date());
        geofenceMapper.insert(geofence);
    }



}
